<?php

namespace App\Domain\Material\Controllers;

use App\Domain\Material\Requests\DelTypePost;
use App\Domain\Material\Requests\DistributionPost;
use App\Domain\Material\Requests\ManualPost;
use App\Domain\Material\Requests\MaterialEditPost;
use App\Domain\Material\Requests\WarehousingPost;
use App\Domain\Material\Services\MaterialService;
use App\Http\Controllers\Controller;
use App\Supports\Constant\PaginationConst;
use App\Supports\Formatter\ResponseFormatter;
use Illuminate\Http\Request;
use Illuminate\Pagination\LengthAwarePaginator;
use Tymon\JWTAuth\Facades\JWTAuth;

class MaterialController extends Controller
{
    //物资列表
    public function list(Request $request, MaterialService $materialService)
    {
        $this->validate($request, [
            'SN' => 'string|nullable',
            'material_dict_code' => 'string|nullable',
            'distribution_status' => 'string|nullable',
            'deliver_status' => 'string|nullable',
            'start_time' => 'string|nullable',
            'end_time' => 'string|nullable',
            'currentPage' => 'integer|min:1|nullable',
            'pageSize' => 'integer|min:1|nullable',
        ]);
        $param = [
            'SN' => $request['SN'],
            'material_dict_code' => $request['material_dict_code'],
            'distribution_status' => $request['distribution_status'],
            'deliver_status' => $request['deliver_status'],
            'start_time' => $request['start_time'],
            'end_time' => $request['end_time'],
            'currentPage' => $request->input('currentPage', 1),
            'pageSize' => $request->input('pageSize', PaginationConst::PAGE_SIZE),
        ];

        $materialPaginator = $materialService->list($param);
        $materialPaginator = new LengthAwarePaginator($materialPaginator->items(), $materialPaginator->total(), $materialPaginator->perPage(), $materialPaginator->currentPage());
        //格式化
        $keyMap = [
            'SN' => 'SN',
            'name' => 'name',
            'material_dict_code' => 'material_dict_code',
            'distribution_status' => 'distribution_status',
            'deliver_status' => 'deliver_status',
            'model' => 'model',
            'bar_code' => 'bar_code',
            'warehousing_time' => 'warehousing_time',
        ];
        return $this->success(ResponseFormatter::formatterPaginator($materialPaginator, $keyMap));
    }

    //物资修改
    public function edit(MaterialEditPost $request, MaterialService $materialService)
    {
        $param = $request->validated();
        $data = $materialService->edit($param);
        return $this->success($data);
    }

    //物资分类及数量
    public function materialNumAndType(MaterialService $materialService)
    {
        $data = $materialService->findNumAndType();
        return $this->success($data);
    }

    //新增更改物资分类
    public function setType(Request $request, MaterialService $materialService)
    {
        $data = $materialService->setType($request);
        return $this->success($data);
    }

    //删除物资分类
    public function delType(DelTypePost $request,MaterialService $materialService){
        $param = $request->validated();
        $data = $materialService->delType($param);
        return $this->success($data);
    }

    //手动入库
    public function Manual(ManualPost $request, MaterialService $materialService)
    {
        $param = $request->validated();
        $data = $materialService->create($param);
        return $this->success($data);
    }


    //扫码入库
    public function warehousing(WarehousingPost $request, MaterialService $materialService)
    {
        $param = $request->validated();
        $data = $materialService->ware($param);
        return $this->success($data);
    }

    //铺货
    public function distribution(DistributionPost $request, MaterialService $materialService)
    {
        $param = $request->validated();
        $param['operator'] = $this->index();
        $data = $materialService->distribution($param);
        return $this->success($data);
    }

    //删除
    public function del(Request $request, MaterialService $materialService)
    {
        $data = $materialService->del($request['SN']);
        return $this->success($data);
    }

    public function index()
    {
        $user = JWTAuth::parseToken()->touser();
        return $user['name'];
    }

    //铺货列表
    public function distributionlist(Request $request, MaterialService $materialService)
    {
        $this->validate($request, [
            'channel_business_id' => 'string|nullable',
            'name' => 'string|nullable',
            'currentPage' => 'integer|min:1|nullable',
            'pageSize' => 'integer|min:1|nullable',
        ]);
        $param = [
            'channel_business_id' => $request['channel_business_id'],
            'name' => $request['name'],
            'currentPage' => $request->input('currentPage', 1),
            'pageSize' => $request->input('pageSize', PaginationConst::PAGE_SIZE),
        ];
        $distributionPaginator = $materialService->distributionlist($param);
//        dd($distributionPaginator->items());
//        $distributionPaginator = new LengthAwarePaginator($distributionPaginator->items(), $distributionPaginator->total(), $distributionPaginator->perPage(), $distributionPaginator->currentPage());
        //格式化
        $keyMap = [
            'name' => 'name',
            'num' => 'num',
            'material_dict_code' => 'material_dict_code',
        ];
        return $this->success(ResponseFormatter::formatterPaginator($distributionPaginator, $keyMap));
    }

    //铺货详情
    public function distributioninfo(Request $request, MaterialService $materialService)
    {
        $this->validate($request, [
            'channel_business_id' => 'string|nullable',
            'name' => 'string|nullable',
            'distribution_status' => 'string|nullable',
            'currentPage' => 'integer|min:1|nullable',
            'pageSize' => 'integer|min:1|nullable',
        ]);
        $param = [
            'channel_business_id' => $request['channel_business_id'],
            'name' => $request['name'],
            'distribution_status' => '1',
            'currentPage' => $request->input('currentPage', 1),
            'pageSize' => $request->input('pageSize', PaginationConst::PAGE_SIZE),
        ];

        $distributionPaginator = $materialService->list($param);
        $distributionPaginator = new LengthAwarePaginator($distributionPaginator->items(), $distributionPaginator->total(), $distributionPaginator->perPage(), $distributionPaginator->currentPage());
        //格式化
        $keyMap = [
            'SN' => 'SN',
            'name' => 'name',
            'material_dict_code' => 'material_dict_code',
            'distribution_status' => 'distribution_status',
            'deliver_status' => 'deliver_status',
            'model' => 'model',
            'bar_code' => 'bar_code',
            'warehousing_time' => 'warehousing_time',
        ];
        return $this->success(ResponseFormatter::formatterPaginator($distributionPaginator, $keyMap));
    }

    //铺货物资删除
    public function distributiondel(Request $request, MaterialService $materialService)
    {
        $param = [
            'material_dict_code' => $request['material_dict_code'],
            'channel_business_id' => $request['channel_business_id'],
        ];
        $data = $materialService->distributiondel($param);
        return $this->success($data);
    }
}
